// 导入
//#region
import React, { Component } from 'react'
import {
  Card,
  Breadcrumb,
  Radio,
  Form,
  Button,
  DatePicker,
  Table,
  Tag,
  Space,
  Modal,
  message,
} from 'antd'
import {
  EditOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined,
} from '@ant-design/icons'
import { Link } from 'react-router-dom'
import Channels from 'components/channels/index'
import { ArticleStatus } from 'api/confong'

import { getarticle, delArticle } from 'api/article'
import defaultImg from 'assets/error.png'
const { RangePicker } = DatePicker
//#endregion
export default class ArticleList extends Component {
  // 页面数据
  //#region
  columns = [
    {
      title: '封面',
      render(data) {
        if (data.cover.type === 0) {
          return (
            <img
              src={defaultImg}
              alt=""
              style={{ width: 200, height: 120, objectFit: 'cover' }}
            ></img>
          )
        } else {
          return (
            <img
              src={data.cover.images[0]}
              alt=""
              style={{ width: 200, height: 120, objectFit: 'cover' }}
            ></img>
          )
        }
      },
    },
    {
      title: '标题',
      dataIndex: 'title',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render(status) {
        const obj = ArticleStatus.find((item) => item.id === status)
        return <Tag color={obj.color}>{obj.name} </Tag>
      },
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate',
    },
    {
      title: '阅读数',
      dataIndex: 'read_count',
    },
    {
      title: '评论数',
      dataIndex: 'comment_count',
    },
    {
      title: '点赞数',
      dataIndex: 'like_count',
    },
    {
      title: '操作',
      render: (data) => {
        return (
          <Space>
            <Button
              type="primary"
              shape="circle"
              icon={<EditOutlined />}
              onClick={() => this.handleEdit(data.id)}
            />
            <Button
              type="primary"
              danger
              shape="circle"
              icon={<DeleteOutlined />}
              onClick={() => this.handleDelete(data.id)}
            />
          </Space>
        )
      },
    },
  ]
  state = {
    //  频道列表数据
    channels: [],
    // 文章列表数据
    articles: {},
  }
  // 文章列表数据参数
  reqParams = {
    page: 1,
    per_page: 10,
  }
  //#endregion
  // 页面渲染
  //#region
  render() {
    const { total_count, results, per_page, page } = this.state.articles
    return (
      <div className="articleList">
        <Card
          title={
            <Breadcrumb separator="/">
              <Breadcrumb.Item>
                <Link to="/home">首页</Link>
              </Breadcrumb.Item>
              <Breadcrumb.Item>文章列表</Breadcrumb.Item>
            </Breadcrumb>
          }
        >
          <Form onFinish={this.onFinish} initialValues={{ status: -1 }}>
            <Form.Item label="状态" name="status">
              <Radio.Group>
                {ArticleStatus.map((item) => {
                  return (
                    <Radio key={item.id} value={item.id}>
                      {item.name}
                    </Radio>
                  )
                })}
              </Radio.Group>
            </Form.Item>
            <Form.Item label="频道" name="channel_id">
              <Channels></Channels>
            </Form.Item>
            <Form.Item label="时间" name="date">
              <RangePicker />
            </Form.Item>
            <Form.Item>
              <Button type="primary" htmlType="submit">
                筛选
              </Button>
            </Form.Item>
          </Form>
        </Card>
        <Card title={`根据筛选条件共查询到${total_count}条数据`}>
          <Table
            dataSource={results}
            columns={this.columns}
            rowKey="id"
            pagination={{
              position: ['bottomCenter'],
              total: total_count,
              pageSize: per_page,
              current: page,
              onChange: this.onChange,
            }}
          />
        </Card>
      </div>
    )
  }
  //#endregion
  // 事件处理
  //#region
  componentDidMount() {
    // 获取文章列表
    this.getarticleList()
  }
  // 修改文章
  handleEdit = (id) => {
    this.props.history.push(`/home/Publish/${id}`)
  }
  // 分页点击
  onChange = (page, pageSize) => {
    console.log(page, pageSize)
    this.reqParams.page = page
    this.reqParams.per_page = pageSize
    this.getarticleList()
  }

  // 获取文章列表
  async getarticleList() {
    const res = await getarticle(this.reqParams)
    this.setState({
      articles: res.data,
    })
  }
  // 点击筛选按钮
  onFinish = ({ status, channel_id, date }) => {
    if (status !== -1) {
      this.reqParams.status = status
    } else {
      delete this.reqParams.status
    }
    if (channel_id !== undefined) {
      this.reqParams.channel_id = channel_id
    } else {
      delete this.reqParams.channel_id
    }
    if (date) {
      this.reqParams.begin_pubdate = date[0]
        .startOf('day')
        .format('YYYY-MM-DD HH:mm:ss')
      this.reqParams.end_pubdate = date[1]
        .endOf('day')
        .format('YYYY-MM-DD HH:mm:ss')
    } else {
      delete this.reqParams.begin_pubdate
      delete this.reqParams.end_pubdate
    }
    this.reqParams.page = 1
    // 获取文章列表
    this.getarticleList()
  }
  // 点击删除按钮
  handleDelete = (id) => {
    Modal.confirm({
      title: '温馨提示？',
      icon: <ExclamationCircleOutlined />,
      content: '你确定要删除文章吗',
      onOk: async () => {
        // 发送请求进行删除
        const res = await delArticle(id)
        console.log(res)
        this.getarticleList()
        message.success('删除成功')
      },
    })
  }
  //#endregion
}
